package com.lg.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.product.entity.Category;
import com.lg.product.mapper.CategoryMapper;
import com.lg.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 服务实现类
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


     /*
      * 查询 分类 列表
      * @param parentId
      * @return
      */
     /*@Override
    public List<Category> listByparentId(Long parentId) {
       List<Category> categories = baseMapper.listByparentId(pid);
         categories.forEach(cat->{
           this.listChId(cat.getId(),cat);
         });
        return categoryList;
    }*/


   /*
     * 递归查询
     * @param id
     * @param cat
     */
   /*
    private void listChId(Long id, Category cat) {
        List<Category> categories = baseMapper.listByparentId(Math.toIntExact(id));
        if(categories.size()>0){
            cat.setCategoryList(categories);
            for (Category cg:categories){
                this.listChId(cg.getId(),cg);
            }
        }
    }*/



    /**
     * 查询 分类 列表
     * @param parentId
     * @return
     */
    @Override
    public List<Category> listByparentId(Long parentId) {
        List<Category> categoryAll = getCategoryAll();
        List<Category> categoryList = getCategoryList(categoryAll, parentId);
        categoryList.forEach(category -> {
            getCate(category,categoryAll);
        });
        return categoryList;
    }

    /**
     * 分类 递归
     * @param category
     * @param categoryList
     */
    public void getCate(Category category,List<Category> categoryList){
        List<Category> categoryList1 = getCategoryList(categoryList, category.getId());
        if(categoryList1.size()>0){
            category.setCategoryList(categoryList1);
            categoryList1.forEach(cate -> {
                getCate(cate,categoryList);
            });
        }
    }

    /**
     * 分类 根据parentId查询列表
     * @param parentId
     * @return
     */
    public List<Category> getCategoryList(Long parentId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",parentId);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     *分类  从categories 查找 getParentId 等于 parentId
     * @param categories
     * @param parentId
     * @return
     */
    public List<Category> getCategoryList(List<Category> categories,Long parentId){
        List<Category> list = new ArrayList<>();
        categories.forEach(cat->{
            if(cat.getParentId().intValue()==parentId.intValue()){
                list.add(cat);
            }
        });
        return list;
    }

    /**
     * 分类 查询所有数据 排序
     * @return
     */
    public List<Category> getCategoryAll(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("sort");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     *  添加 分类
     * @param category
     * @return
     */
    @Override
    public int addCategory(Category category) {
        return baseMapper.addCategory(category);
    }

    /**
     *  分类 回显
     * @param id
     * @return
     */
    public Category listId(Integer id){
        Category category = baseMapper.selectById(id);
        return category;
    }

    /**
     * 分类 修改
     * @param category
     * @return
     */
    public int updateCategory(Category category){
        baseMapper.updateById(category);
        return 1;
    }

    /**
     * 分类 删除
     * @return
     */
    public int deleteCategory(Integer id){
        return baseMapper.deleteById(id);
    }

}
